package org.itboys.pay.weixin;

import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.random.RandomUtils;
import org.itboys.pay.constant.PayConstants;

/**
 * js支付  签名
 * @author huml
 *
 */
public class WeixinPaySignTool {
	
	private String timeStamp;//时间戳
	private String nonceStr;//随机字符串
	private String paySign;//最终得到的签名
	private String appId;
	private String package_;//统一下单接口返回的prepay_id参数值，提交格式如：prepay_id=***
	private String signType = PayConstants.ENCRYPT_MD5;//签名方式
	private String codeUrl;//扫码支付时返回的链接
	private Long objId;
	/**
	 * 实例化时完成加密
	 * @param appId
	 * @param package_
	 */
	public WeixinPaySignTool(String appId , String package_, String key){
		this.timeStamp = Long.toString(System.currentTimeMillis()/1000);
		this.nonceStr = RandomUtils.getClearRandomStr(16);
		this.appId = appId;
		this.package_ = "prepay_id="+package_;

		String param = "appId="+this.appId+"&nonceStr="+this.nonceStr+"&package="+this.package_
				+"&signType="+this.signType+"&timeStamp="+this.timeStamp+"&key="+key;
		this.paySign = Digests.md5(param).toUpperCase();
	}
	
	public String getTimeStamp() {
		return timeStamp;
	}
	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}
	public String getNonceStr() {
		return nonceStr;
	}
	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}
	public String getPaySign() {
		return paySign;
	}
	public void setPaySign(String paySign) {
		this.paySign = paySign;
	}
	public String getAppId() {
		return appId;
	}
	public void setAppId(String appId) {
		this.appId = appId;
	}
	public String getPackage_() {
		return package_;
	}
	public void setPackage_(String package_) {
		this.package_ = package_;
	}
	public String getSignType() {
		return signType;
	}
	public void setSignType(String signType) {
		this.signType = signType;
	}

	@Override
	public String toString() {
		return "WeixinPaySignTool [timeStamp=" + timeStamp + ", nonceStr=" + nonceStr + ", paySign=" + paySign + ", appId=" + appId + ", package_=" + package_ + ", signType=" + signType + "]";
	}


	public Long getObjId() {
		return objId;
	}

	public void setObjId(Long objId) {
		this.objId = objId;
	}

	public String getCodeUrl() {
		return codeUrl;
	}

	public void setCodeUrl(String codeUrl) {
		this.codeUrl = codeUrl;
	}
}
